package cn.itheima.action;

import cn.itheima.domain.Courier;
import cn.itheima.service.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by millions on 2017/7/3.
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier>{

    private Courier courier=new Courier();

    @Override
    public Courier getModel() {
        return courier;
    }

    @Autowired
    private CourierService courierService;

    @Action(value = "courier_save",results = @Result(type = "redirect",location = "./pages/base/courier.html"))
    public String saveCourier(){
        courierService.save(courier);

        return SUCCESS;
    }

    private int rows;
    private int page;

    public void setRows(int rows) {
        this.rows = rows;
    }

    public void setPage(int page) {
        this.page = page;
    }

    @Action(value = "query_byPage",results = {@Result(type = "json")})
    public String queryCourierByPage(){

        Pageable pageable=new PageRequest(page-1<0?0:page-1,rows==0?10:rows);

        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list=new ArrayList<>();
                if(StringUtils.isNotBlank(courier.getCourierNum())){
                    Predicate p1 = cb.equal(root.get("courierNum").as(String.class), courier.getCourierNum());
                    list.add(p1);
                }
                if(StringUtils.isNotBlank(courier.getCompany())){
                    Predicate p2 = cb.equal(root.get("company").as(String.class), "%" + courier.getCompany() + "%");
                    list.add(p2);
                }
                if(StringUtils.isNotBlank(courier.getType())){
                    Predicate p3 = cb.equal(root.get("type").as(String.class), courier.getType());
                    list.add(p3);
                }
                Join<Object, Object> join = root.join("standard", JoinType.INNER);
                if(null != courier.getStandard() && StringUtils.isNotBlank(courier.getStandard().getName())){
                    Predicate p4 = cb.like(join.get("name").as(String.class), "%" + courier.getStandard().getName()+"%");
                    list.add(p4);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };

        Page<Courier> pageData=courierService.findPageData(pageable,specification);

        // 将返回page对象 转换datagrid需要格式
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageData.getTotalElements());
        result.put("rows", pageData.getContent());
        // 将结果对象 压入值栈顶部
        ActionContext.getContext().getValueStack().push(result);

        return SUCCESS;
    }

    private String ids;

    public void setIds(String ids) {
        this.ids = ids;
    }

    @Action(value = "courier_delBatch",results = {@Result(type = "redirect",location = "./pages/base/courier.html")})
    public String batchDel(){

        String[] split = ids.split(",");
        courierService.delBatch(split);

        return SUCCESS;
    }


    /**
     * 获取为关联定区的快递员
     * @return
     */
    @Action(value = "courier_findnoassociation",results = {@Result(type = "json")})
    public String courierFindNoAssociation(){

        List<Courier> couriers=courierService.findNoAssociation();

        ActionContext.getContext().getValueStack().push(couriers);
        return SUCCESS;
    }
}
